package com.xw.childhealth.controller;

import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.NeonatalVisit;
import com.xw.childhealth.enums.ResultEnum;
import com.xw.childhealth.service.NeonatalVisitService;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.Map;

/**
 * @Description: 新生儿家庭访视
 * @Author: y
 * @CreateDate: 2019-12-03 14:26
 * @Version: 1.0
 */
@RequestMapping("/neonatalVisit")
@RestController
public class NeonatalVisitController {

    @Autowired
    private NeonatalVisitService neonatalVisitService;


    /**
     * 查询新生儿家庭访视
     *
     * @param size
     * @param page
     * @param request
     * @return
     */
    @PostMapping("/findNeonatalVisit")
    public ResultVO<?> findNeonatalVisit(@RequestParam(value = "size", defaultValue = "10") Integer size, @RequestParam(value = "page", defaultValue = "0") Integer page, HttpServletRequest request) {

        Page<NeonatalVisit> all = neonatalVisitService.findAll(page, size, request);

        return ResultVOUtil.success(all);
    }

    /**
     * 增加或修改新生儿家庭访视
     *
     * @param id
     * @param type
     * @param map
     * @return
     */
    @Transactional
    @RequestMapping("/data-upd")
    public ModelAndView edit(NeonatalVisit neonatalVisit, @RequestParam(value = "id", required = false) Integer id, @RequestParam(value = "type", required = false) String type,
                             Map<String, Object> map) {
        if (type == null)
            return new ModelAndView("/pages/admin/neonatal-visit", map);

        if (type.equals("toUpd")) {
            NeonatalVisit result = neonatalVisitService.findOneById(id);
            map.put("type", type);
            map.put("result", result);
        } else if (type.equals("upd")) {
            NeonatalVisit result = neonatalVisitService.findOneById(id);
            neonatalVisit.setCreateTime(result.getCreateTime());
            neonatalVisit.setUpdateTime(new Date());
            BeanUtils.copyProperties(neonatalVisit, result);
            neonatalVisitService.save(result);
            return new ModelAndView("/pages/admin/neonatal-visit", map);
        }
        if (type.equals("add")) {
            NeonatalVisit save = neonatalVisitService.save(neonatalVisit.setCreateTime(new Date()).setUpdateTime(new Date()));
            return new ModelAndView("/pages/admin/neonatal-visit", map);
        } else if (type.equals("toAdd")) {
            map.put("result", new NeonatalVisit().setId(0));
        }
        return new ModelAndView("/pages/admin/neonatal-visit-add", map);
    }


    @PostMapping("/data-del")
    public ResultVO dataDel(@RequestParam("id") Integer id) {
        NeonatalVisit result = neonatalVisitService.findOneById(id);
        if (result != null) {
            neonatalVisitService.delById(id);
            return ResultVOUtil.success();
        }
        return ResultVOUtil.error(ResultEnum.DEL_ERR);
    }

}
